package cn.nawang.ebeim.client.util;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.PropertyConfigurator;

public class SystemUtil {

  private static final String CONF_PATH = "conf";
  private static final String BIN_PATH = "bin";

  public static String getRootPath() throws Exception {
    String path =  pathEncode(Class.class.getClass().getResource("/").getPath());
    return path;
  }
  
  public static String getRootPath(Object obj) throws Exception {
    String path =  pathEncode(obj.getClass().getResource("/").getPath());
    return path;
  }

  public static String getConfPath() throws Exception {
    String rootPath = getRootPath();
    return rootPath + getChangePath(rootPath) + CONF_PATH;
  }
  
  public static String getConfPath(Object obj) throws Exception {
    String rootPath = getRootPath(obj);
    return rootPath + getChangePath(rootPath) + CONF_PATH;
  }
  
  public static String getBinPath(Object obj) throws Exception {
    String rootPath = getRootPath(obj);
    return rootPath + getChangePath(rootPath) + BIN_PATH;
  }
  
  public static String getChangePath(String path) {
    if (path.endsWith("classes") || path.endsWith("classes/")) {
      return "../../";
    }
    return "../";
  }

  public static void initLog() throws Exception {
    File rootDir = new File(PathUtil.getClassPath());
    String path = rootDir + "/others/conf/log4jEbeimclient.properties";
    path = java.net.URLDecoder.decode(path,"utf-8");//
    PropertyConfigurator.configure(path);
  }
  
  public static String pathEncode(String path) throws UnsupportedEncodingException{
    String configPath = path;
    configPath = java.net.URLDecoder.decode(configPath,"utf-8");
    return configPath;
  }

  public static void main(String[] args) throws Exception {
    System.out.println(SystemUtil.getConfPath());
  }

}
